package cn.bj.yu.list;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 周宇
 * @create 2020-08-17 16:45
 */
public class ServiceDemo {
    public static void main(String[] args) {
        //获取100台设备
        List<DeviceEntity> deviceEntities = getAllDevices();
        //当前时间
        long currentTimeMillis = System.currentTimeMillis();
        //创建线程池8个
        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(8);
        //遍历
        for (DeviceEntity deviceEntity : deviceEntities) {
            ChildThread childThread = new ChildThread(deviceEntity);
            newFixedThreadPool.submit(childThread);
        }
        newFixedThreadPool.shutdown();

        long currentTimeMillis2 = System.currentTimeMillis();
        System.out.println("查询了" + (currentTimeMillis2 - currentTimeMillis) + "毫秒");
    }

    //模拟根据设备信息获取设备运行时间
    public static void getDeviceRunTime(DeviceEntity deviceEntity) {
        deviceEntity.setRunTime("运行了" + deviceEntity.getDeviceId() + "天");
        //模拟接口阻塞时间100毫秒
        try {
            Thread.sleep(100L);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //假设有100台设备
    public static List<DeviceEntity> getAllDevices() {
        List<DeviceEntity> deviceEntities = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            DeviceEntity deviceEntity = new DeviceEntity();
            deviceEntity.setDeviceId(i);
            deviceEntity.setDeviceName("设备" + i);
            deviceEntity.setDeviceIp("192.168.100." + i);
            deviceEntities.add(deviceEntity);
        }
        return deviceEntities;
    }
}

class ChildThread implements Runnable{

    private DeviceEntity  deviceEntity;

    public ChildThread(DeviceEntity deviceEntity) {
        super();
        this.deviceEntity = deviceEntity;
    }

    @Override
    public void run() {
        ServiceDemo.getDeviceRunTime(deviceEntity);
    }

}
